<template>
    <div v-mydialogDrag class="my-dialog__wrapper">
            <div class="my-dialog" >
                <div class="my-dialog__header" >
                    <slot name="title">
                        <span class="el-dialog__title">Title</span>
                    </slot>
                </div>
                <div class="my-dialog__body">
                    <el-tabs v-model="nodeDisplayParams.type" type="card" @tab-click="handleClick">
                        <el-tab-pane :label="nodeDisplayParams.label" name="node">
                            <el-form label-position="right" label-width="70px" :model="selectPort"  :rules="rules" ref="selectPort">
                                <el-form-item label="x">
                                    <el-input v-model="selectPort.x"></el-input>
                                </el-form-item>
                                <el-form-item label="y">
                                    <el-input v-model="selectPort.y"></el-input>
                                </el-form-item>
                                <el-form-item label="width">
                                    <el-input v-model="selectPort.width"></el-input>
                                </el-form-item>
                                <el-form-item label="height">
                                    <el-input v-model="selectPort.height"></el-input>
                                </el-form-item>
                                <el-form-item :label="nodeDisplayParams.label_num" prop="num"><!--v-if="nodeDisplayParams.num"-->
                                    <el-input v-model="selectPort.num"></el-input>
                                </el-form-item>
                                <el-form-item label="PortType" v-if="selectPort.type == 'port'">
                                    <el-select v-model="selectPort.portType" :placeholder="$t('pleaseSelect')">
                                        <el-option
                                            v-for="item in allPortTypes"
                                            :key="item.portTypeId"
                                            :label="item.portTypeName"
                                            :value="item.portTypeId">
                                        </el-option>
                                    </el-select>
                                </el-form-item>
                                <el-form-item label="CardType" v-if="selectPort.type == 'card'"><!--v-if="nodeDisplayParams.num"-->
                                    <el-input v-model="selectPort.portType" placeholder="no input no card xml"></el-input>
                                </el-form-item>
                                <el-form-item label="LedType" v-if="selectPort.type == 'led'"><!--v-if="nodeDisplayParams.num"-->
                                    <el-select v-model="selectPort.portType" :placeholder="$t('pleaseSelect')">
                                        <el-option
                                            v-for="item in allLedTypes"
                                            :key="item.ledTypeId"
                                            :label="item.ledTypeName"
                                            :value="item.ledTypeId">
                                        </el-option>
                                    </el-select>
                                </el-form-item>
                                <div v-if="selectPort.type == 'card'">
                                    <el-form-item label="IsMenus">
                                        <el-switch
                                            v-model="selectPort.isMenus"
                                            inactive-text="false"
                                            active-text="true">
                                        </el-switch>
                                    </el-form-item>
                                    <el-form-item label="CardMenu" v-if="selectPort.isMenus">
                                        <el-select v-model="selectPort.CardMenu" multiple placeholder="the same as shelf">
                                            <el-option
                                                v-for="item in allCardMenus"
                                                :key="item.cardMenuId"
                                                :label="item.cardMenuId"
                                                :value="item.cardMenuId">
                                            </el-option>
                                        </el-select>
                                    </el-form-item>
                                    <el-form-item label="PortMenu" v-if="selectPort.isMenus">
                                        <el-select v-model="selectPort.PortMenu" multiple placeholder="the same as shelf">
                                            <!--<el-option-->
                                                <!--v-for="item in allPortTypes"-->
                                                <!--:key="item.portTypeId"-->
                                                <!--:label="item.portTypeName"-->
                                                <!--:value="item.portTypeId">-->
                                            <!--</el-option>-->
                                            <el-option-group
                                                v-for="group in allPortTypes"
                                                :key="group.label"
                                                :label="group.label">
                                                <el-option
                                                    v-for="item in group.options"
                                                    :key="item.value"
                                                    :label="item.label"
                                                    :value="item.value">
                                                </el-option>
                                            </el-option-group>
                                        </el-select>
                                    </el-form-item>
                                </div>
                            </el-form>

                            <div align="center">
                                <el-button size="small" type="primary" @click="changeSelectPortPos">确定</el-button>
                                <el-button size="small" type="danger" @click="deleteSelectPort">删除</el-button>
                            </div>
                        </el-tab-pane>
                        <el-tab-pane label="机架" name="shelf">
                            <el-form label-position="right" label-width="70px">
                                <el-form-item label="Title">
                                    <el-input v-model="shelfParams.Title"></el-input>
                                </el-form-item>
                                <el-form-item label="ShelfName">
                                    <!--<el-input v-model="shelfParams.ShelfName"></el-input>-->
                                    <el-select v-model="shelfParams.ShelfName" clearable :placeholder="$t('pleaseSelect')">
                                        <el-option
                                            v-for="item in allShelfNames"
                                            :key="item.shelfName"
                                            :label="item.shelfName"
                                            :value="item.shelfName">
                                        </el-option>
                                    </el-select>
                                </el-form-item>
                                <el-form-item label="IsPolling">
                                    <el-switch
                                        v-model="shelfParams.IsPolling"
                                        inactive-text="false"
                                        active-text="true">
                                    </el-switch>
                                </el-form-item>
                                <el-form-item label="CardPollingTime" v-if="shelfParams.IsPolling">
                                    <el-input type="number" v-model="shelfParams.CardPollingTime"></el-input>
                                </el-form-item>
                                <el-form-item label="PortPollingTime" v-if="shelfParams.IsPolling">
                                    <el-input type="number" v-model="shelfParams.PortPollingTime"></el-input>
                                </el-form-item>
                                <el-form-item label="SlotDIF">
                                    <el-input type="number" v-model="shelfParams.SlotDIF"></el-input>
                                </el-form-item>
                                <el-form-item label="CardMenu">
                                    <el-select v-model="shelfParams.CardMenu" multiple :placeholder="$t('pleaseSelect')">
                                        <el-option
                                            v-for="item in allCardMenus"
                                            :key="item.cardMenuId"
                                            :label="item.cardMenuId"
                                            :value="item.cardMenuId">
                                        </el-option>
                                    </el-select>
                                </el-form-item>
                                <el-form-item label="PortMenu">
                                    <el-select v-model="shelfParams.PortMenu" multiple :placeholder="$t('pleaseSelect')">
                                        <!--<el-option-->
                                            <!--v-for="item in allPortTypes"-->
                                            <!--:key="item.portTypeId"-->
                                            <!--:label="item.portTypeName"-->
                                            <!--:value="item.portTypeId">-->
                                        <!--</el-option>-->
                                        <el-option-group
                                            v-for="group in allPortTypes"
                                            :key="group.label"
                                            :label="group.label">
                                            <el-option
                                                v-for="item in group.options"
                                                :key="item.value"
                                                :label="item.label"
                                                :value="item.value">
                                            </el-option>
                                        </el-option-group>
                                    </el-select>

                                </el-form-item>

                            </el-form>
                        </el-tab-pane>
                    </el-tabs>
                </div>
            </div>
    </div>
</template>

<script>
    export default {
        props : {
            dialogParams: {},
            selectPort: {},
            shelfParams: {},
        },
        data() {
            var validateNull = (rule, value, callback) => {
                if (value == '') {
                    callback(new Error("can't be null"));
                } else {
                    callback();
                }
            };
            return {
                allPortTypes : [
                    {
                        label: 'port type 1',
                        options: [{
                            value: '1_PortStatus',
                            label: '1 - port status'
                        }]
                    },
                    {
                        label: 'port type 2',
                        options: [{
                            value: '2_PortStatus',
                            label: '2 - port status'
                        }]
                    },
                    {
                        label: 'port type 3',
                        options: [{
                            value: '3_PortStatus',
                            label: '3 - port status'
                        }]
                    },
                    {
                        label: 'port type 4',
                        options: [{
                            value: '4_PortStatus',
                            label: '4 - port status'
                        }]
                    },
                    {
                        label: 'port type 5',
                        options: [{
                            value: '5_PortStatus',
                            label: '5 - port status'
                        }]
                    },
                    {
                        label: 'port type 9',
                        options: [{
                            value: '9_PortStatus',
                            label: '9 - port status'
                        }]
                    },
                ],
                nodeDisplayParams : {
                    type : 'shelf',
                    label : '端口',
                    label_num : 'num',
                    label_type : 'Type'
                },
                // allPortTypes : [
                //     {
                //         portTypeId : 1,
                //         portTypeName : 1,
                //     },
                //     {
                //         portTypeId : 2,
                //         portTypeName : 2,
                //     },
                //     {
                //         portTypeId : 3,
                //         portTypeName : 3,
                //     },
                //     {
                //         portTypeId : 4,
                //         portTypeName : 4,
                //     },
                //     {
                //         portTypeId : 5,
                //         portTypeName : 5,
                //     },
                //     {
                //         portTypeId : 9,
                //         portTypeName : 9,
                //     },
                // ],
                allLedTypes : [
                    {
                        ledTypeId : 'port',
                        ledTypeName : 'port',
                    },
                    {
                        ledTypeId : 'poe',
                        ledTypeName : 'poe',
                    },
                    {
                        ledTypeId : 'slot',
                        ledTypeName : 'slot',
                    },
                    {
                        ledTypeId : 'sys',
                        ledTypeName : 'sys',
                    },
                    {
                        ledTypeId : 'pwr1',
                        ledTypeName : 'pwr1',
                    },
                    {
                        ledTypeId : 'pwr2',
                        ledTypeName : 'pwr2',
                    },{
                        ledTypeId : 'pwr3',
                        ledTypeName : 'pwr3',
                    },{
                        ledTypeId : 'pwr4',
                        ledTypeName : 'pwr4',
                    },{
                        ledTypeId : 'pwr5',
                        ledTypeName : 'pwr5',
                    },



                ],
                allCardMenus : [
                    {
                        cardMenuId : 'MenuCardReset',
                        cardMenuName : 'resetMenuCard()',
                    },
                    {
                        cardMenuId : 'MenuCardDisable',
                        cardMenuName : 'setMenuCardDisable()',
                    },
                    {
                        cardMenuId : 'MenuCardEnable',
                        cardMenuName : 'setMenuCardEnable()',
                    },
                ],
                allShelfNames : [
                    {
                        shelfName : 'MRD_TOM',
                    },
                    {
                        shelfName : 'ONLY_IFTABLE',
                    },
                    {
                        shelfName : 'RESET_ONE_CARD',
                    },
                    {
                        shelfName : 'DIRECT_IFTABLE',
                    },
                ],

                rules: {
                    num : [{required: true, validator : validateNull, trigger: 'blur' }],
                },
            }
        },
        watch:{
            selectNodeTypeMonitor(){
                let vm = this;
                if(vm.selectPort.type == 'port'){
                    vm.nodeDisplayParams.label = '端口';
                    vm.nodeDisplayParams.label_num = 'PortNum';
                    vm.nodeDisplayParams.label_type = 'PortType';
                }
                else if(vm.selectPort.type == 'card'){
                    vm.nodeDisplayParams.label = '板卡';
                    vm.nodeDisplayParams.label_num = 'SlotNum';
                    vm.nodeDisplayParams.label_type = 'CardType';
                }
                else if(vm.selectPort.type == 'led'){
                    vm.nodeDisplayParams.label = '指示灯';
                    vm.nodeDisplayParams.label_num = 'LedNum';
                }
                else if(vm.selectPort.type == 'power'){
                    vm.nodeDisplayParams.label = '电源';
                    vm.nodeDisplayParams.label_num = 'PowerNum';
                }
            },
            selectNodeIdMonitor(){
                let vm = this;
                vm.nodeDisplayParams.type = 'node'
            },
        },
        computed: {
            selectNodeTypeMonitor(){
                let vm = this;
                return vm.selectPort.type;
            },
            selectNodeIdMonitor(){
                let vm = this;
                return vm.selectPort.id;
            }
        },
        methods: {
            handleClick(tab, event) {
                let vm = this;
                vm.nodeDisplayParams.type = tab.paneName;
            },
            changeSelectPortPos() {
                let vm = this
                vm.$refs.selectPort.validate((valid) => {
                    if (valid) {
                        vm.$parent.changeSelectPortPos();
                    } else {
                        return false;
                    }
                });

            },
            deleteSelectPort() {
                let vm = this;
                vm.$parent.deleteSelectPort();
            },
        },
        created: function () {
            let vm = this
        },
        mounted(){
            let vm = this
        }
    }

    import Vue from 'vue';
    // v-dialogDrag: 弹窗拖拽
    Vue.directive('mydialogDrag', {
        bind(el, binding, vnode, oldVnode) {
            const dialogHeaderEl = el.querySelector('.my-dialog__header');
            const dragDom = el.querySelector('.my-dialog');
            dialogHeaderEl.style.cursor = 'move';

            // 获取原有属性 ie dom元素.currentStyle 火狐谷歌 window.getComputedStyle(dom元素, null);
            const sty = dragDom.currentStyle || window.getComputedStyle(dragDom, null);

            dialogHeaderEl.onmousedown = (e) => {
                // 鼠标按下，计算当前元素距离可视区的距离
                const disX = e.clientX - dialogHeaderEl.offsetLeft;
                const disY = e.clientY - dialogHeaderEl.offsetTop;

                // 获取到的值带px 正则匹配替换
                let styL, styT;

                // 注意在ie中 第一次获取到的值为组件自带50% 移动之后赋值为px
                if(sty.left.includes('%')) {
                    styL = +document.body.clientWidth * (+sty.left.replace(/\%/g, '') / 100);
                    styT = +document.body.clientHeight * (+sty.top.replace(/\%/g, '') / 100);
                }else {
                    styL = +sty.left.replace(/\px/g, '');
                    styT = +sty.top.replace(/\px/g, '');
                };

                document.onmousemove = function (e) {
                    // 通过事件委托，计算移动的距离
                    const l = e.clientX - disX;
                    const t = e.clientY - disY;

                    // 移动当前元素
                    dragDom.style.left = `${l + styL}px`;
                    dragDom.style.top = `${t + styT}px`;

                    //将此时的位置传出去
                    //binding.value({x:e.pageX,y:e.pageY})
                };

                document.onmouseup = function (e) {
                    document.onmousemove = null;
                    document.onmouseup = null;
                };
            }
        }
    })
</script>
<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
    .my-dialog .el-input{
        /*width: 90%;*/
    }
    .my-dialog{
        position:fixed;
        margin:0 auto 50px;
        background:#fff;
        border-radius:2px;
        -webkit-box-shadow:0 1px 3px rgba(0,0,0,.3);
        box-shadow:0 1px 3px rgba(0,0,0,.3);
        box-sizing:border-box;
        /*right:57px;*/
        /*top: 206px;*/
    }
    .my-dialog__wrapper{
        position:fixed;
        z-index: 1;
        right:367px;
        top: 206px;
    }
    .my-dialog__header{
        padding:10px 10px 10px;
        text-align:center
    }
    .my-dialog__body{
        padding:0px 10px 10px 10px;
        color:#606266;
        line-height:24px;
        font-size:14px
    }
</style>
